home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / More Source / Pascal / Skel 3.0i / Skel HighLevelEvents.p < prev    next >
Text File  |  1994-04-18  |  3KB  |  92 lines

  1. unit HighLevelEvents;
  2.  
  3. interface
  4.     uses
  5.         AppleEvents, Globals;
  6.  
  7.     procedure DoHighLevelEvent (e: eventRecord);
  8.     procedure DoAppleEvent (e: EventRecord);
  9.  
  10. implementation
  11.  
  12. {############################   High Level Events   #############################}
  13.  
  14. { Handle high level events. We handle the following high level events:}
  15. {- The standard MultiFinder events, suspend and resume.}
  16. {- The required Apple Events, Open Application, Open Document, Print Document and QuitApplication.}
  17. {All except Quit Application merely reports that they havn't done anything.}
  18. {Entire unit added by LIR}
  19.  
  20.  
  21.  
  22. {Handle the required Apple events:}
  23. {DoOpenApp,DoOpenDoc,DoPrintDoc,DoQuitApp}
  24. {MyGotRequiredParams: From MSG demo my Mark Pilgrim, tells whether we have handled all we have to or not.}
  25.     function MyGotRequiredParams (theAppleEvent: AppleEvent): OSErr;
  26.         var
  27.             returnedType: DescType;
  28.             actualSize: Size;
  29.     begin
  30.         if AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard, returnedType, nil, 0, actualSize) = errAEDescNotFound then
  31.             MyGotRequiredParams := noErr
  32.         else
  33.             MyGotRequiredParams := errAEParamMissed;
  34.     end;
  35.     function DoOpenApp (theAppleEvent, reply: AppleEvent; refCon: Longint): OSErr;
  36.     begin
  37. {What am I supposed to do here?}
  38.         DoOpenApp := MyGotRequiredParams(theAppleEvent);
  39.     end;
  40.     function DoOpenDoc (theAppleEvent, reply: AppleEvent; refCon: Longint): OSErr;
  41.     begin
  42.         DoOpenDoc := errAEEventNotHandled; {We don't open any documents!}
  43.     end;
  44.     function DoPrintDoc (theAppleEvent, reply: AppleEvent; refCon: Longint): OSErr;
  45.     begin
  46.         DoPrintDoc := errAEEventNotHandled; {We don't print any documents!}
  47.     end;
  48.     function DoQuitApp (theAppleEvent, reply: AppleEvent; refCon: Longint): OSErr;
  49.     begin
  50.         userdone := true;            {If I'm told to quit, I'll quit.}
  51.         DoQuitApp := MyGotRequiredParams(theAppleEvent);
  52.     end;
  53.  
  54. {Init Apple events}
  55. {Perhaps I'm cheating, but I don't call this until I get the first Apple event.}
  56. {IMHO, that's the simplest way to support them without a lot of boring Gestalt checks.}
  57.     procedure AppleEventInit;
  58.         var
  59.             error: OSerr;
  60.     begin
  61.         if gAppleEventsInitialized then
  62.             exit(AppleEventInit);
  63.         gAppleEventsInitialized := true;
  64.         error := AEInstallEventHandler(kCoreEventClass, kAEOpenApplication, @DoOpenApp, 0, false);
  65.         error := AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, @DoOpenDoc, 0, false);
  66.         error := AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments, @DoPrintDoc, 0, false);
  67.         error := AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, @DoQuitApp, 0, false);
  68. {I ignore errors.}
  69.     end;
  70.  
  71. {Handle suspend or resume events.}
  72. {Here, I choose to use suspend/resume events to change gSleep. Increasing gSleep when we are}
  73. {in the background reduces processing load.}
  74.     procedure DoHighLevelEvent (e: eventRecord);
  75.     begin
  76.         if BAND(BROTL(e.message, 8), $FF) = SuspendResumeMessage then
  77.             if BAnd(e.message, 1) <> 0 then
  78.                 gSleep := 5 {Resume event}
  79.             else
  80.                 gSleep := 30; {Suspend event}
  81.     end;
  82.  
  83. {Handle Apple Events}
  84.     procedure DoAppleEvent (e: EventRecord);
  85.     begin
  86.         if not gAppleEventsInitialized then {My little "cheat" into compatibility}
  87.             AppleEventInit;
  88.         if AEProcessAppleEvent(e) <> noErr then
  89.             ;
  90.     end;
  91.  
  92. end.